"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 防篡改
Case Name   : 验证手动锁定的用户不会自动解锁
Create At   : 2022-03-10
Owner       : opentestcase041
Description :
    1.修改以下参数值
    enableSeparationOfDuty=on
    failed_login_attempts=1
     password_lock_time=0.00011574074074074074074074074074075
    2.创建用户
    3.手动锁定用户
    4.查看用户是否被解锁
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.未被解锁处于锁定状态
    5.成功
History     : 
"""



import os
import unittest
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Logger import Logger


class ModifyCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.user_name = 'u_security_userlockprivilege_0009'
        self.default_value1 = self.common.show_param('enableSeparationOfDuty')
        self.default_value2 = self.common.show_param('failed_login_attempts')
        self.default_value3 = self.common.show_param('password_lock_time')

    def test_security(self):
        text = '----step1:修改以下参数值;expect: 修改成功----'
        self.log.info(text)
        param_value = ['enableSeparationOfDuty=on', 'failed_login_attempts=1',
                       'password_lock_time='
                       '0.00011574074074074074074074074074075']
        for value in param_value:
            msg = \
                self.primary_sh.execute_gsguc('set',
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              f'{value}')
            self.assertTrue(msg, '执行失败:' + text)
        restart_msg = self.primary_sh.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)

        text = '-----step2：创建用户 expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop user if exists {self.user_name} cascade; " \
            f"create user {self.user_name}  password '{macro.PASSWD_REPLACE}';"
        msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, msg,
                      '执行失败:' + text)

        text = '-----step3：手动锁定用户 expect:成功-----'
        self.log.info(text)
        sql_cmd = self.primary_sh.execut_db_sql(
            f'''alter user {self.user_name} account lock;
            select pg_sleep(10);''')
        self.log.info(sql_cmd)
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG, sql_cmd,
                      '执行失败:' + text)

        text = '----step4:登录数据库查看用户是否被解锁 ' \
               'expect:未被解锁处于锁定状态----'
        self.log.info(text)
        sql_cmd = f'''select user;'''
        self.log.info(sql_cmd)
        msg = self.primary_sh.execut_db_sql(
            sql_cmd, sql_type=f' -U {self.user_name} '
            f'-W {macro.PASSWD_REPLACE}')
        self.log.info(msg)
        self.assertIn(self.constant.ACCOUNT_LOCKED_TYPE, msg,
                      "执行失败:" + text)

    def tearDown(self):
        text = '----step5:清理环境 expect:成功----'
        self.log.info(text)
        self.log.info('删除用户')
        sql_cmd = self.primary_sh.execut_db_sql(
            f"drop user {self.user_name} cascade; ")
        self.log.info(sql_cmd)
        self.log.info('恢复默认值')
        guc_res1 = \
            self.primary_sh.execute_gsguc('set',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          f'enableSeparationOfDuty = '
                                          f'{self.default_value1}')
        self.log.info(guc_res1)
        guc_res2 = \
            self.primary_sh.execute_gsguc('set',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          f'failed_login_attempts = '
                                          f'{self.default_value2}')
        self.log.info(guc_res2)
        guc_res3 = \
            self.primary_sh.execute_gsguc('set',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          f'password_lock_time = '
                                          f'{self.default_value3}')
        self.log.info(guc_res3)
        restart_msg = self.primary_sh.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.primary_sh.get_db_cluster_status()
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, sql_cmd,
                      '执行失败:' + text)
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)
        self.assertTrue(guc_res1, '执行失败:' + text)
        self.assertTrue(guc_res2, '执行失败:' + text)
        self.assertTrue(guc_res3, '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
